home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / INTERNET / SITES / LITTLE / P3SRC.ZIP / ATARI / PLANES.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-08  |  12.2 KB  |  687 lines

  1. /****************************************************************************
  2. *                planes.c
  3. *
  4. *  This module implements functions that manipulate planes.
  5. *
  6. *  from Persistence of Vision(tm) Ray Tracer
  7. *  Copyright 1996 Persistence of Vision Team
  8. *---------------------------------------------------------------------------
  9. *  NOTICE: This source code file is provided so that users may experiment
  10. *  with enhancements to POV-Ray and to port the software to platforms other
  11. *  than those supported by the POV-Ray Team.  There are strict rules under
  12. *  which you are permitted to use this file.  The rules are in the file
  13. *  named POVLEGAL.DOC which should be distributed with this file. If
  14. *  POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  15. *  Team Coordinator by leaving a message in CompuServe's Graphics Developer's
  16. *  Forum.  The latest version of POV-Ray may be found there as well.
  17. *
  18. * This program is based on the popular DKB raytracer version 2.12.
  19. * DKBTrace was originally written by David K. Buck.
  20. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  21. *
  22. *****************************************************************************/
  23.  
  24. #include "frame.h"
  25. #include "povray.h"
  26. #include "vector.h"
  27. #include "povproto.h"
  28. #include "matrices.h"
  29. #include "objects.h"
  30. #include "planes.h"
  31.  
  32.  
  33.  
  34. /*****************************************************************************
  35. * Local preprocessor defines
  36. ******************************************************************************/
  37.  
  38. #define DEPTH_TOLERANCE 1.0e-6
  39.  
  40. /*****************************************************************************
  41. * Static functions
  42. ******************************************************************************/
  43.  
  44. static int   Intersect_Plane PARAMS((RAY *Ray, PLANE *Plane, DBL *Depth));
  45. static int   All_Plane_Intersections PARAMS((OBJECT *Object, RAY *Ray, ISTACK *Depth_Stack));
  46. static int   Inside_Plane PARAMS((VECTOR point, OBJECT *Object));
  47. static void  Plane_Normal PARAMS((VECTOR Result, OBJECT *Object, INTERSECTION *Inter));
  48. static void  *Copy_Plane PARAMS((OBJECT *Object));
  49. static void  Translate_Plane PARAMS((OBJECT *Object, VECTOR Vector, TRANSFORM *Trans));
  50. static void  Rotate_Plane PARAMS((OBJECT *Object, VECTOR Vector, TRANSFORM *Trans));
  51. static void  Scale_Plane PARAMS((OBJECT *Object, VECTOR Vector, TRANSFORM *Trans));
  52. static void  Transform_Plane PARAMS((OBJECT *Object, TRANSFORM *Trans));
  53. static void  Invert_Plane PARAMS((OBJECT *Object));
  54. static void  Destroy_Plane PARAMS((OBJECT *Object));
  55.  
  56. /*****************************************************************************
  57. * Local variables
  58. ******************************************************************************/
  59.  
  60. METHODS Plane_Methods =
  61. {
  62.   All_Plane_Intersections,
  63.   Inside_Plane, Plane_Normal,
  64.   Copy_Plane,
  65.   Translate_Plane, Rotate_Plane,
  66.   Scale_Plane, Transform_Plane, Invert_Plane, Destroy_Plane
  67. };
  68.  
  69.  
  70. /*****************************************************************************
  71. *
  72. * FUNCTION
  73. *
  74. *   All_Plane_Intersections
  75. *
  76. * INPUT
  77. *   
  78. * OUTPUT
  79. *   
  80. * RETURNS
  81. *   
  82. * AUTHOR
  83. *
  84. *   POV-Ray Team
  85. *   
  86. * DESCRIPTION
  87. *
  88. *   -
  89. *
  90. * CHANGES
  91. *
  92. *   -
  93. *
  94. ******************************************************************************/
  95.  
  96. static int All_Plane_Intersections (Object, Ray, Depth_Stack)
  97. OBJECT *Object;
  98. RAY *Ray;
  99. ISTACK *Depth_Stack;
  100. {
  101.   DBL Depth;
  102.   VECTOR IPoint;
  103.  
  104.   if (Intersect_Plane(Ray, (PLANE *)Object, &Depth))
  105.   {
  106.     VEvaluateRay(IPoint, Ray->Initial, Depth, Ray->Direction);
  107.  
  108.     if (Point_In_Clip(IPoint, Object->Clip))
  109.     {
  110.       push_entry(Depth,IPoint,Object,Depth_Stack);
  111.  
  112.       return(TRUE);
  113.     }
  114.   }
  115.  
  116.   return(FALSE);
  117. }
  118.  
  119.  
  120.  
  121. /*****************************************************************************
  122. *
  123. * FUNCTION
  124. *
  125. *   Intersect_Plane
  126. *
  127. * INPUT
  128. *   
  129. * OUTPUT
  130. *   
  131. * RETURNS
  132. *   
  133. * AUTHOR
  134. *
  135. *   POV-Ray Team
  136. *   
  137. * DESCRIPTION
  138. *
  139. *   -
  140. *
  141. * CHANGES
  142. *
  143. *   -
  144. *
  145. ******************************************************************************/
  146.  
  147. static int Intersect_Plane (Ray, Plane, Depth)
  148. RAY *Ray;
  149. PLANE *Plane;
  150. DBL *Depth;
  151. {
  152.   DBL NormalDotOrigin, NormalDotDirection;
  153.   VECTOR P, D;
  154.  
  155.   Increase_Counter(stats[Ray_Plane_Tests]);
  156.  
  157.   if (Plane->Trans == NULL)
  158.   {
  159.     VDot(NormalDotDirection, Plane->Normal_Vector, Ray->Direction);
  160.  
  161.     if (fabs(NormalDotDirection) < EPSILON)
  162.     {
  163.       return(FALSE);
  164.     }
  165.  
  166.     VDot(NormalDotOrigin, Plane->Normal_Vector, Ray->Initial);
  167.   }
  168.   else
  169.   {
  170.     MInvTransPoint(P, Ray->Initial, Plane->Trans);
  171.     MInvTransDirection(D, Ray->Direction, Plane->Trans);
  172.  
  173.     VDot(NormalDotDirection, Plane->Normal_Vector, D);
  174.  
  175.     if (fabs(NormalDotDirection) < EPSILON)
  176.     {
  177.       return(FALSE);
  178.     }
  179.  
  180.     VDot(NormalDotOrigin, Plane->Normal_Vector, P);
  181.   }
  182.  
  183.   *Depth = -(NormalDotOrigin + Plane->Distance) / NormalDotDirection;
  184.  
  185.   if ((*Depth >= DEPTH_TOLERANCE) && (*Depth <= Max_Distance))
  186.   {
  187.     Increase_Counter(stats[Ray_Plane_Tests_Succeeded]);
  188.  
  189.     return (TRUE);
  190.   }
  191.   else
  192.   {
  193.     return (FALSE);
  194.   }
  195. }
  196.  
  197.  
  198.  
  199. /*****************************************************************************
  200. *
  201. * FUNCTION
  202. *
  203. *   Inside_Plane
  204. *
  205. * INPUT
  206. *   
  207. * OUTPUT
  208. *   
  209. * RETURNS
  210. *   
  211. * AUTHOR
  212. *
  213. *   POV-Ray Team
  214. *   
  215. * DESCRIPTION
  216. *
  217. *   -
  218. *
  219. * CHANGES
  220. *
  221. *   -
  222. *
  223. ******************************************************************************/
  224.  
  225. static int Inside_Plane (IPoint, Object)
  226. VECTOR IPoint;
  227. OBJECT *Object;
  228. {
  229.   DBL Temp;
  230.   VECTOR P;
  231.  
  232.   if (((PLANE *)Object)->Trans == NULL)
  233.   {
  234.     VDot (Temp, IPoint, ((PLANE *)Object)->Normal_Vector);
  235.   }
  236.   else
  237.   {
  238.     MInvTransPoint(P, IPoint, ((PLANE *)Object)->Trans);
  239.  
  240.     VDot (Temp, P, ((PLANE *)Object)->Normal_Vector);
  241.   }
  242.  
  243.   return((Temp + ((PLANE *)Object)->Distance) < EPSILON);
  244. }
  245.  
  246.  
  247.  
  248. /*****************************************************************************
  249. *
  250. * FUNCTION
  251. *
  252. *   Plane_Normal
  253. *
  254. * INPUT
  255. *
  256. * OUTPUT
  257. *
  258. * RETURNS
  259. *
  260. * AUTHOR
  261. *
  262. *   POV-Ray Team
  263. *
  264. * DESCRIPTION
  265. *
  266. *   -
  267. *
  268. * CHANGES
  269. *
  270. *   -
  271. *
  272. ******************************************************************************/
  273.  
  274. static void Plane_Normal (Result, Object, Inter)
  275. OBJECT *Object;
  276. VECTOR Result;
  277. INTERSECTION *Inter;
  278. {
  279.   Assign_Vector(Result,((PLANE *)Object)->Normal_Vector);
  280.  
  281.   if (((PLANE *)Object)->Trans != NULL)
  282.   {
  283.     MTransNormal(Result, Result, ((PLANE *)Object)->Trans);
  284.  
  285.     VNormalize(Result, Result);
  286.   }
  287. }
  288.  
  289.  
  290.  
  291. /*****************************************************************************
  292. *
  293. * FUNCTION
  294. *
  295. *   Translate_Plane
  296. *
  297. * INPUT
  298. *   
  299. * OUTPUT
  300. *   
  301. * RETURNS
  302. *   
  303. * AUTHOR
  304. *
  305. *   POV-Ray Team
  306. *   
  307. * DESCRIPTION
  308. *
  309. *   -
  310. *
  311. * CHANGES
  312. *
  313. *   -
  314. *
  315. ******************************************************************************/
  316.  
  317. static void Translate_Plane (Object, Vector, Trans)
  318. OBJECT *Object;
  319. VECTOR Vector;
  320. TRANSFORM *Trans;
  321. {
  322.   VECTOR Translation;
  323.   PLANE *Plane = (PLANE *)Object;
  324.  
  325.   if (Plane->Trans == NULL)
  326.   {
  327.     VEvaluate (Translation, ((PLANE *)Object)->Normal_Vector, Vector);
  328.  
  329.     ((PLANE *)Object)->Distance -= Translation[X] + Translation[Y] + Translation[Z];
  330.  
  331.     Compute_Plane_BBox((PLANE *)Object);
  332.   }
  333.   else
  334.   {
  335.     Transform_Plane(Object, Trans);
  336.   }
  337. }
  338.  
  339.  
  340.  
  341. /*****************************************************************************
  342. *
  343. * FUNCTION
  344. *
  345. *   Rotate_Plane
  346. *
  347. * INPUT
  348. *   
  349. * OUTPUT
  350. *   
  351. * RETURNS
  352. *   
  353. * AUTHOR
  354. *
  355. *   POV-Ray Team
  356. *   
  357. * DESCRIPTION
  358. *
  359. *   -
  360. *
  361. * CHANGES
  362. *
  363. *   -
  364. *
  365. ******************************************************************************/
  366.  
  367. static void Rotate_Plane (Object, Vector, Trans)
  368. OBJECT *Object;
  369. VECTOR Vector;
  370. TRANSFORM *Trans;
  371. {
  372.   if (((PLANE *)Object)->Trans == NULL)
  373.   {
  374.     MTransDirection(((PLANE *)Object)->Normal_Vector, ((PLANE *)Object)->Normal_Vector, Trans);
  375.  
  376.     Compute_Plane_BBox(((PLANE *)Object));
  377.   }
  378.   else
  379.   {
  380.     Transform_Plane (Object, Trans);
  381.   }
  382. }
  383.  
  384.  
  385.  
  386. /*****************************************************************************
  387. *
  388. * FUNCTION
  389. *
  390. *   Scale_Plane
  391. *
  392. * INPUT
  393. *   
  394. * OUTPUT
  395. *   
  396. * RETURNS
  397. *   
  398. * AUTHOR
  399. *
  400. *   POV-Ray Team
  401. *   
  402. * DESCRIPTION
  403. *
  404. *   -
  405. *
  406. * CHANGES
  407. *
  408. *   -
  409. *
  410. ******************************************************************************/
  411.  
  412. static void Scale_Plane (Object, Vector, Trans)
  413. OBJECT *Object;
  414. VECTOR Vector;
  415. TRANSFORM *Trans;
  416. {
  417.   DBL Length;
  418.   PLANE *Plane = (PLANE  *) Object;
  419.  
  420.   if (Plane->Trans == NULL)
  421.   {
  422.     VDivEq(Plane->Normal_Vector, Vector);
  423.  
  424.     VLength(Length, ((PLANE *)Object)->Normal_Vector);
  425.  
  426.     VInverseScaleEq (((PLANE *)Object)->Normal_Vector, Length);
  427.  
  428.     ((PLANE *)Object)->Distance /= Length;
  429.  
  430.     Compute_Plane_BBox(Plane);
  431.   }
  432.   else
  433.   {
  434.     Transform_Plane (Object, Trans);
  435.   }
  436. }
  437.  
  438.  
  439.  
  440. /*****************************************************************************
  441. *
  442. * FUNCTION
  443. *
  444. *   Invert_Plane
  445. *
  446. * INPUT
  447. *
  448. * OUTPUT
  449. *
  450. * RETURNS
  451. *
  452. * AUTHOR
  453. *
  454. *   POV-Ray Team
  455. *
  456. * DESCRIPTION
  457. *
  458. *   -
  459. *
  460. * CHANGES
  461. *
  462. *   -
  463. *
  464. ******************************************************************************/
  465.  
  466. static void Invert_Plane (Object)
  467. OBJECT *Object;
  468. {
  469.   VScaleEq(((PLANE *)Object)->Normal_Vector, -1.0);
  470.  
  471.   ((PLANE *)Object)->Distance *= -1.0;
  472. }
  473.  
  474.  
  475.  
  476. /*****************************************************************************
  477. *
  478. * FUNCTION
  479. *
  480. *   Transform_Plane
  481. *
  482. * INPUT
  483. *
  484. * OUTPUT
  485. *
  486. * RETURNS
  487. *
  488. * AUTHOR
  489. *
  490. *   POV-Ray Team
  491. *
  492. * DESCRIPTION
  493. *
  494. *   -
  495. *
  496. * CHANGES
  497. *
  498. *   -
  499. *
  500. ******************************************************************************/
  501.  
  502. static void Transform_Plane(Object, Trans)
  503. OBJECT *Object;
  504. TRANSFORM *Trans;
  505. {
  506.   PLANE *Plane = (PLANE  *) Object;
  507.  
  508.   if (Plane->Trans == NULL)
  509.   {
  510.     Plane->Trans = Create_Transform();
  511.   }
  512.  
  513.   Compose_Transforms(Plane->Trans, Trans);
  514.  
  515.   Compute_Plane_BBox(Plane);
  516. }
  517.  
  518.  
  519.  
  520. /*****************************************************************************
  521. *
  522. * FUNCTION
  523. *
  524. *   Create_Plane
  525. *
  526. * INPUT
  527. *
  528. * OUTPUT
  529. *
  530. * RETURNS
  531. *
  532. * AUTHOR
  533. *
  534. *   POV-Ray Team
  535. *   
  536. * DESCRIPTION
  537. *
  538. *   -
  539. *
  540. * CHANGES
  541. *
  542. *   -
  543. *
  544. ******************************************************************************/
  545.  
  546. PLANE *Create_Plane()
  547. {
  548.   PLANE *New;
  549.  
  550.   New = (PLANE *)POV_MALLOC(sizeof (PLANE), "plane");
  551.  
  552.   INIT_OBJECT_FIELDS(New,PLANE_OBJECT,&Plane_Methods)
  553.  
  554.   Make_Vector(New->Normal_Vector, 0.0, 1.0, 0.0);
  555.  
  556.   New ->Distance = 0.0;
  557.  
  558.   New->Trans = NULL;
  559.  
  560.   return(New);
  561. }
  562.  
  563.  
  564.  
  565. /*****************************************************************************
  566. *
  567. * FUNCTION
  568. *
  569. *   Copy_Plane
  570. *
  571. * INPUT
  572. *   
  573. * OUTPUT
  574. *   
  575. * RETURNS
  576. *   
  577. * AUTHOR
  578. *
  579. *   POV-Ray Team
  580. *   
  581. * DESCRIPTION
  582. *
  583. *   -
  584. *
  585. * CHANGES
  586. *
  587. *   -
  588. *
  589. ******************************************************************************/
  590.  
  591. static void *Copy_Plane (Object)
  592. OBJECT *Object;
  593. {
  594.   PLANE *New;
  595.  
  596.   New = Create_Plane();
  597.  
  598.   Destroy_Transform(New->Trans);
  599.  
  600.   *New = *((PLANE *)Object);
  601.  
  602.   New->Trans = Copy_Transform(((PLANE *)Object)->Trans);
  603.  
  604.   return(New);
  605. }
  606.  
  607.  
  608.  
  609. /*****************************************************************************
  610. *
  611. * FUNCTION
  612. *
  613. *   Destroy_Plane
  614. *
  615. * INPUT
  616. *   
  617. * OUTPUT
  618. *   
  619. * RETURNS
  620. *   
  621. * AUTHOR
  622. *
  623. *   POV-Ray Team
  624. *   
  625. * DESCRIPTION
  626. *
  627. *   -
  628. *
  629. * CHANGES
  630. *
  631. *   -
  632. *
  633. ******************************************************************************/
  634.  
  635. static void Destroy_Plane(Object)
  636. OBJECT *Object;
  637. {
  638.   Destroy_Transform(((PLANE *)Object)->Trans);
  639.  
  640.   POV_FREE(Object);
  641. }
  642.  
  643.  
  644.  
  645. /*****************************************************************************
  646. *
  647. * FUNCTION
  648. *
  649. *   Compute_Plane_BBox
  650. *
  651. * INPUT
  652. *
  653. *   Plane - Plane
  654. *   
  655. * OUTPUT
  656. *
  657. *   Plane
  658. *   
  659. * RETURNS
  660. *   
  661. * AUTHOR
  662. *
  663. *   Dieter Bayer
  664. *   
  665. * DESCRIPTION
  666. *
  667. *   Calculate the bounding box of a plane (it's always infinite).
  668. *
  669. * CHANGES
  670. *
  671. *   Aug 1994 : Creation.
  672. *
  673. ******************************************************************************/
  674.  
  675. void Compute_Plane_BBox(Plane)
  676. PLANE *Plane;
  677. {
  678.   Make_BBox(Plane->BBox, -BOUND_HUGE/2, -BOUND_HUGE/2, -BOUND_HUGE/2,
  679.     BOUND_HUGE, BOUND_HUGE, BOUND_HUGE);
  680.  
  681.   if (Plane->Clip != NULL)
  682.   {
  683.     Plane->BBox = Plane->Clip->BBox;
  684.   }
  685. }
  686.  
  687.